package com.cjl.demo.domain;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 *域名有效性检测
 */
public class HttpMain {

    public static void main(String[] args) {
        String logPath = "D:\\Desktop-11\\微信域名大作战\\赶集域名\\third_ganji_23400.txt-原始";
//        String logPath = "D:\\second_ganji_7500.txt";
        readLog(logPath);
    }

    private static boolean checkUrl(String url){
        String realUrl = url + "/admin/delete/1";
        realUrl.toCharArray();
        try {
            String doResult = doGet(realUrl);
            JSONObject result = JSON.parseObject(doResult);
            if (result.getIntValue("statusCode") == 1) {
//                System.out.println("接口访问成功+++++++++" + realUrl);
                return true;
            }
            System.out.println("接口访问失败=========" + realUrl);
        } catch (Exception e) {
            System.out.println("接口访问异常########" + realUrl);
//            e.printStackTrace();
        }
        return false;
    }

    private static void readLog(String logPath){
        List<String> urlList = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(logPath)), "UTF-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                if (null != line) {
                    String url = "https://" + line ;
                    boolean checkResult = checkUrl(url);
                    if(!checkResult){
                        System.out.println(line);
                    } else {
                        urlList.add(url);
                        System.out.println("urlList.size = [" + urlList.size() + "]");
                    }
                }
            }
            System.out.println("finish - " + urlList.size());
            br.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }
    }

    private static String doGet(String url) throws Exception {
        URL localURL = new URL(url);
        URLConnection connection = localURL.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;

        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuilder resultBuffer = new StringBuilder();
        String tempLine = null;

        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }

        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);

            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }

        } finally {

            if (reader != null) {
                reader.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }

        }

        return resultBuffer.toString();
    }

    private static String doPost(String url) throws Exception {
        String parameterData = "test=test&2=2";

        URL localURL = new URL(url);
        URLConnection connection = localURL.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;

        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.length()));

        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuilder resultBuffer = new StringBuilder();
        String tempLine = null;

        try {
            outputStream = httpURLConnection.getOutputStream();
            outputStreamWriter = new OutputStreamWriter(outputStream);

            outputStreamWriter.write(parameterData.toString());
            outputStreamWriter.flush();

            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }

            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);

            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }

        } finally {

            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            if (reader != null) {
                reader.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }

        }

        return resultBuffer.toString();
    }

}
